package com.study.rpc;

import com.study.rpc.entity.Request;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @version V1.0
 * @title: SocketClientProxy
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2019-11-11 17:44
 * @copyright: http://fingo.com.my/
 */
public class SocketClientProxy {

    private SocketClient socketClient = new SocketClient();

    public <T> T getProxy(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class<?>[]{clazz}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Request request = new Request();
                        request.setClassName(method.getDeclaringClass().getName());
                        request.setMethodName(method.getName());
                        request.setParamTypes(method.getParameterTypes());
                        request.setParams(args);
                        return socketClient.invoke(request, "127.0.0.1", 12000);
                    }
                });
    }

}
